from selenium import webdriver
from time import sleep
from chrome_webdriver import get_webdriver
from selenium.webdriver import ActionChains  # 动作链类
from selenium.webdriver.chrome.options import Options  # 五可视化界面
from selenium.webdriver import ChromeOptions  # 规避检测

from selenium.webdriver.common.by import By

a = [
    By.ID,
    By.NAME,
    By.CLASS_NAME,
    By.LINK_TEXT,
    By.TAG_NAME,
    By.CSS_SELECTOR,
    By.PARTIAL_LINK_TEXT,
    By.XPATH,
]

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(executable_path=path,chrome_options=chrome_options)

if __name__ == '__main__':
    # 获取驱动位置
    path = get_webdriver('Chrome', '79')

    driver = webdriver.Chrome(executable_path=path)
    # url = 'https://www.baidu.com/'
    # url = 'http://localhost:8080/environment'
    url = 'http://localhost:8080/'
    js = "window.localStorage.setItem('userInfo','{\"id\":1,\"username\":\"jiang\",\"head_img\":null,\"name\":null,\"sex\":null,\"age\":null,\"status\":2,\"access_token\":\"a1da8426-e543-442c-9cdb-8638d4caeb1d\"}')"
    js2 = "window.localStorage.setItem('userInfo','{aaa}')".format(
        aaa='{"id":1,"username":"jiang","head_img":null,"name":null,"sex":null,"age":null,"status":2,"access_token":"a1da8426-e543-442c-9cdb-8638d4caeb1d"}')
    print({'a': js2})
    aa = driver.get(url)
    sleep(3)
    driver.execute_script(js2)
    driver.refresh()

    print(driver.get_cookies())
    driver.get('http://localhost:8080/environment')

    # div = driver.find_element_by_id('kw')
    # div.send_keys('元尊')
    # driver.find_element_by_id('su').click()
    # sleep(3)
    # lis = driver.find_elements_by_xpath('//h3/a')
    # for i in lis:
    #     print(i.text)
    # lis = driver.find_element_by_xpath('//h3/a')
    # print(lis)

    sleep(50)
    driver.quit()
